import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 公司
 * @date: 2018-7-16
 */
import { getResponse } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api'; // import { fetchCountryList } from '../services/countryService';

import { fetchCompany as _fetchCompany, enableCompany as _enableCompany, disableCompany as _disableCompany, queryCompany as _queryCompany, createCompany as _createCompany, queryProvinceCity as _queryProvinceCity, fetchCountryList } from "../services/companyService";
export default {
  namespace: 'company',
  state: {
    countryList: [],
    // 国家列表
    companyType: [],
    // 企业类型值集
    taxpayerType: [],
    // 纳税人标识
    companyList: [],
    companyDetail: {}
  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, vl, res;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryMapIdpValue, {
                companyType: 'CPFM.COMPANY_TYPE',
                taxpayerType: 'CPFM.TAXPAYER_TYPE'
              });

            case 4:
              vl = _context.sent;
              _context.next = 7;
              return call(fetchCountryList, payload);

            case 7:
              res = _context.sent;
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  companyType: vl.companyType || [],
                  taxpayerType: vl.taxpayerType || [],
                  countryList: res
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    queryProvinceCity: /*#__PURE__*/_regeneratorRuntime.mark(function queryProvinceCity(_ref3, _ref4) {
      var payload, call, put, cityList, safeCityList;
      return _regeneratorRuntime.wrap(function queryProvinceCity$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_queryProvinceCity, payload);

            case 4:
              cityList = _context2.sent;
              safeCityList = getResponse(cityList);

              if (!safeCityList) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  cityList: cityList
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, queryProvinceCity);
    }),
    // 获取公司信息
    fetchCompany: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCompany(_ref5, _ref6) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchCompany$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context3.next = 4;
              return call(_fetchCompany, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  companyList: list
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchCompany);
    }),
    // 设置公司启用
    enableCompany: /*#__PURE__*/_regeneratorRuntime.mark(function enableCompany(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function enableCompany$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_enableCompany, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, enableCompany);
    }),
    // 设置公司禁用
    disableCompany: /*#__PURE__*/_regeneratorRuntime.mark(function disableCompany(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function disableCompany$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_disableCompany, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, disableCompany);
    }),
    // 根据id查询公司
    queryCompany: /*#__PURE__*/_regeneratorRuntime.mark(function queryCompany(_ref11, _ref12) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function queryCompany$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context6.next = 4;
              return call(_queryCompany, payload);

            case 4:
              res = _context6.sent;
              list = getResponse(res);

              if (!list) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  companyDetail: list
                }
              });

            case 9:
              return _context6.abrupt("return", list);

            case 10:
            case "end":
              return _context6.stop();
          }
        }
      }, queryCompany);
    }),
    // 新建公司
    createCompany: /*#__PURE__*/_regeneratorRuntime.mark(function createCompany(_ref13, _ref14) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createCompany$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              _context7.next = 4;
              return call(_createCompany, payload);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, createCompany);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref15) {
      var payload = _ref15.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};